const {
  queryAllArticles,
  queryArticleByIdService,
  addService,
  modifyService,
  deleteServiceById,
  deleteServiceByIds,
  queryByUserIdService,
  commentByIdService,
  addNewsService
} = require("../service/article.service");
const request = require('superagent');  //发送请求模块
const cheerio = require("cheerio");
const Article = require("../model/article.model");
class ArticleController {
  async queryArticles(ctx, next) {
    const requestObj = ctx.request.body;
    let page = requestObj && requestObj.page ? parseInt(requestObj.page) : 1;
    let size = requestObj && requestObj.size ? parseInt(requestObj.size) : 10;
    console.log(requestObj, "--queryall--articles--Control");

    const res = await queryAllArticles(requestObj, page, size);
    ctx.body = {
      code: 0,
      messgae: "success",
      data: res.data,
      total: res.total,
      currentPage: page,
      pageSize: size,
      totalPage: Math.ceil(res.total / size),
    };
  }

  // 按id查询
  async queryArticleById(ctx, next) {
    // id未传值
    if (!ctx.request.query.id) {
      ctx.status = 400;
      ctx.body = { message: "无id" };
      return;
    }
    const res = await queryArticleByIdService(ctx.request.query);
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: res[0],
    };
  }

  // 添加文章
  async addControl(ctx, next) {
    const requestParams = { ...ctx.request.body };
    if(!requestParams.userId) {
      requestParams.userId = ctx.user.id;
    }
    const res = await addService(requestParams);
    console.log("res", res);
    ctx.body = {
      code: 200,
      message: "添加成功!",
      result: null,
    };
  }
  // 按id修改文章
  async modifyControl(ctx, next) {
    const requestParams = { ...ctx.request.body };
    const res = await modifyService(requestParams);

    ctx.body = {
      code: 200,
      message: "修改成功!",
      result: null,
    };
  }

  // 按id删除文章
  async deleteByControl(ctx, next) {
    try {
      const { id } = ctx.query;
      console.log(id, "iddd");
      await deleteServiceById(id);
      ctx.body = {
        code: 200,
        message: "删除成功",
        data: null,
      };
    } catch (error) {
      console.log(error, "error");
      ctx.body = {
        code: 400,
        message: "删除失败！",
        data: null,
      };
    }
  }

  async deleteByIdsControl(ctx,next){
    const {ids} = ctx.request.body;
    await deleteServiceByIds(ids);
    ctx.body = {
      code:200,
      message:"删除成功",
      data:null,
    }
  }


  // 按用户id查找文章
  async queryByUserId(ctx,next) {
    const requestObj = ctx.request.body;
    const res = await queryByUserIdService(requestObj);
    ctx.body = {
      code:200,
      message:"查询成功",
      data:res,
    }
  }

  async clawControl(ctx,next) {
    const url = ctx.request.body.url;

    const res =await request.get(url);
    console.log(res,"请求结果")
    const $ = cheerio.load(res.text);
    let title = $('#articleContentId').text();
    let content = $('#content_views').html().trim();
    let requestObj = {
      title,
      summary:title,
      tagsId:null,
      typeId:null,
      isTop:0,
      status:0,
      composeType:0,
      link:url,
      isRecommend:0,
      isBanner:0,
      userId:3,
      titleImg:null,
      contentMd:content,
    };
    const addState = await addService(requestObj);
    ctx.body = content;
  }

  // 按formId查询所有评论
  async commentByIdControl(ctx,next) {
    console.log(ctx.query,"query")
    let {id} = ctx.query;
   const res  = await commentByIdService(id);
   ctx.body = {
    code:200,
    message:"success",
    data:res,
   }
  }

  // 添加评论
  async addNewsControl(ctx,next) {
    // userId,content,forumId,
    let obj = ctx.request.body;

    await addNewsService(obj);
    ctx.body = {
      code:200,
      message:"success",
    }
  }
}

module.exports = new ArticleController();
